package com.open.system.dispatch.impl;

import com.open.base.utils.UserHolderUtils;
import com.open.system.dispatch.INoticeDispatch;
import com.open.system.model.Notice;
import com.open.common.model.Page;
import com.open.system.model.UserNotice;
import com.open.system.model.dto.SendDto;
import com.open.system.model.vo.NoticeVo;
import com.open.system.service.INoticeService;
import com.open.system.service.IUserNoticeService;
import com.open.common.utils.DateUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.List;

/**
 * 
 *
 * @author 大树03
 * @date 2025-07-18 17:38
 */
@Component(value="noticeDispatch")
@Slf4j
public class NoticeDispatchImpl implements INoticeDispatch{

	private INoticeService noticeService;

    @Autowired
	public void setNoticeService(INoticeService noticeService) {
		this.noticeService = noticeService;
	}

	@Autowired
	public IUserNoticeService userNoticeService;

	@Override
	public Boolean save(Notice model)  {
		return noticeService.save(model);
	}

	@Override
	public Boolean update(Notice model)  {
		return noticeService.update(model);
	}

	@Override
	public Page<Notice> findPage(Page<Notice> page,Notice item)  {
		return noticeService.findPage(page,item);
	}
	
	@Override
	public Notice findByPk(Long pk) {
		return noticeService.findByPk(pk);
	}

	@Override
    public List<Notice> findByModel(Notice model)  {
		return noticeService.findByModel(model);
	}

	@Override
	public Boolean remove(List<Long> pks) {
		return noticeService.remove(pks);
	}

	@Override
	public Boolean send(SendDto sendDto) {
		List<UserNotice> lists = new ArrayList<>();
		sendDto.getUserIds().forEach(userId -> {
			UserNotice userNotice = new UserNotice();
			BeanUtils.copyProperties(sendDto, userNotice);
			if (sendDto.getSendType().equals(1)) {
				userNotice.setSendTime(DateUtils.getCurrentTimeAsString());
			}
			userNotice.setRead(0);
			userNotice.setUserId(userId);
			lists.add(userNotice);
		});
		return userNoticeService.batchSave(lists);
	}

	@Override
	public Integer unread() {
		UserNotice item = new UserNotice();
		item.setUserId(UserHolderUtils.getUserId());
		item.setRead(0);
		item.setSendTime(DateUtils.getCurrentTimeAsString());
		return userNoticeService.countByModel(item);
	}

    @Override
    public Page<NoticeVo> scrollPage(Page<UserNotice> page, UserNotice model) {
		return noticeService.scrollPage(page, model);
    }
}